package com.can.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.can.reggie.common.R;
import com.can.reggie.dto.SetmealDto;
import com.can.reggie.entity.Category;
import com.can.reggie.entity.Setmeal;
import com.can.reggie.service.CategoryService;
import com.can.reggie.service.SetmealDishService;
import com.can.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)//allEntries:代表删除 setmealCache下所有缓存
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }


    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //分页构造器对象
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        //添加查询条件，1.未被删除,2.根据name进行like模糊查询,3.添加排序条件，4.根据更新时间降序排列
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getIsDeleted, 0);
        lqw.like(name != null, Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        //查询并返回给setmealPage
        setmealService.page(setmealPage, lqw);

        //将setmealPage 拷贝进 setmealDtoPage,除了records
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");

        //new 一个 SetmealDto类型的list
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        //遍历setmealPage中的records
        List<Setmeal> records = setmealPage.getRecords();
        for (Setmeal record : records) {
            //new一个setmealDto
            SetmealDto setmealDto = new SetmealDto();
            //拷贝record至setmealDto
            BeanUtils.copyProperties(record, setmealDto);

            //根据record中的菜品id查询出菜品名称
            Category category = categoryService.getById(record.getCategoryId());
            String categoryName = category.getName();

            //将菜品名称封装进setmealDto,再将setmealDto封装进setmealDtoList
            setmealDto.setCategoryName(categoryName);
            setmealDtoList.add(setmealDto);
        }

        //将setmealDtoList 封装进setmealDtoPage
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }


    /**
     * 删除套餐(设置id_deleted为1)
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)//allEntries:代表删除 setmealCache下所有缓存
    public R<String> delete(@RequestParam List<Long> ids) {

        //删除套餐,同时删除套餐和菜品的关联数据
        setmealService.removeWithDish(ids);

        return R.success("套餐删除成功");
    }


    /**
     * 根据条件查询套餐数据
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getIsDeleted, 0);   //未被删除
//        queryWrapper.like(StringUtils.isNotEmpty(setmeal.getName()), Setmeal::getName, setmeal.getName());
        queryWrapper.eq(null != setmeal.getCategoryId(), Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(null != setmeal.getStatus(), Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        return R.success(setmealService.list(queryWrapper));
    }


    /**
     * 根据id修改套餐状态
     *
     * @param ids
     * @param stateId
     * @return
     */
    @PostMapping("/status/{stateId}")
    public R<String> statusUpdate(Long[] ids, @PathVariable Integer stateId) {
        //遍历ids
        for (Long id : ids) {
            //根据id,更改套餐状态
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(stateId);
            setmeal.setId(id);

            setmealService.updateById(setmeal);
        }

        return R.success("套餐状态修改成功");
    }


    /**
     * 根据id查询套餐所有信息(回显)
     *
     * @param setmealId
     * @return
     */
    @GetMapping("/{setmealId}")
    public R<SetmealDto> getOne(@PathVariable Long setmealId) {

        SetmealDto setmealDto = setmealService.getData(setmealId);

        return R.success(setmealDto);
    }


    /**
     * 根据id,修改套餐
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> updateById(@RequestBody SetmealDto setmealDto){

        setmealService.updateSetmealWithDish(setmealDto);

        return R.success("套餐修改成功");
    }

}



